﻿/**
 * Viewport2D
 * 
 * @version 2012.01.27
 * @author  Bruno Miguel de Oliveira Tamer <bmotamer@gmail.com>
 */

#region C# Libraries

using System.Collections.Generic;

#endregion

#region XNA Libraries

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace main.sdk.display.object2d
{

    public sealed class Viewport2D : DisplayObject
    {

        #region Instance Variables

        public SpriteSortMode    SortMode          = SpriteSortMode.Immediate;
        public BlendState        BlendState        = BlendState.AlphaBlend;
        public SamplerState      SamplerState      = SamplerState.PointWrap;
        public DepthStencilState DepthStencilState = null;
        public RasterizerState   RasterizerState   = RasterizerState.CullNone;
        public Effect            Effect            = null;
        public Matrix            TransformMatrix   = Matrix.Identity;

        private readonly SpriteBatch    SpriteBatch = new SpriteBatch(Program.Self.GraphicsDevice);
        private readonly List<Object2D> Children    = new List<Object2D>();

        #endregion

        #region Constructor & Destructor

        public Viewport2D() { Display.AddViewport2D(this); }

        public void Dispose()
        {
            Children.Clear();
            SpriteBatch.Dispose();
            Display.AddViewport2D(this);
        }

        #endregion

        #region Add & Remove

        public void Add(Object2D child) { Children.Add(child); }

        public void Remove(Object2D child) { Children.Remove(child); }

        #endregion

        #region Drawing

        public void Draw()
        {
            SpriteBatch.Begin(SortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, TransformMatrix);
            foreach (Object2D child in Children)
                if (child.Visible)
                    child.Draw(SpriteBatch);
            SpriteBatch.End();
        }

        #endregion

    }

}